package com.dxq.test.common;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;

import androidx.fragment.app.FragmentActivity;

import java.util.Stack;

/**
 * Author: zsh
 * Date: 2021/5/21
 * Describe:应用程序Activity管理类：用于Activity管理和应用程序退出
 *  可以让所有的activity都继承BaseActivity然后给activity在onCreate中添加到栈中onDestroyed中移除
 *  <p>
 *  添加Activity到堆栈
 *  AppManager.getAppManager().addActivity(this);
 *  结束Activity&从堆栈中移除
 *  AppManager.getAppManager().finishActivity(this);
 */
public class AppManager {

    private static Stack<FragmentActivity> activityStack;
    private static Stack<Service> serviceStack;
    private static AppManager instance;

    private AppManager() {
    }

    /**
     * 单一实例
     */
    public static synchronized AppManager getAppManager() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(FragmentActivity activity) {
        if (activityStack == null) {
            activityStack = new Stack<>();
        }
        activityStack.add(activity);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity currentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        Activity activity = activityStack.lastElement();
        finishActivity(activity);
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if (null != activityStack && activityStack.size() > 0) {
            for (Activity activity : activityStack) {
                if (null != activity) {
                    activity.finish();
                }
            }
            activityStack.clear();
        }
    }

    /**
     * 添加Service到堆栈
     */
    public void addService(Service service) {
        if (serviceStack == null) {
            serviceStack = new Stack<Service>();
        }
        serviceStack.add(service);
    }

    /**
     * 获取当前Service（堆栈中最后一个压入的）
     */
    public Service currentService() {
        Service service = serviceStack.lastElement();
        return service;
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishService() {
        Service service = serviceStack.lastElement();
        finishService(service);
    }

    /**
     * 结束指定的Service
     */
    public void finishService(Service service) {
        if (service != null) {
            serviceStack.remove(service);
            service.stopSelf();
            service = null;
        }
    }

    /**
     * 结束指定类名的Service
     */
    public void finishService(Class<?> cls) {
        for (Service service : serviceStack) {
            if (service.getClass().equals(cls)) {
                finishService(service);
            }
        }
    }

    /**
     * 结束所有Service
     */
    public void finishAllService() {
        if (null != serviceStack && serviceStack.size() > 0) {
            for (int i = 0, size = serviceStack.size(); i < size; i++) {
                if (null != serviceStack.get(i)) {
                    finishService(serviceStack.get(i));
                }
            }
        }
    }

    /**
     * 退出应用程序
     */
    public void appExit(Context context) {
        try {

            finishAllActivity();
            finishAllService();
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            //activityManager.restartPackage(mContext.getPackageName());
            activityManager.killBackgroundProcesses(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
            //LogUtils.e(e.getMessage());
        }
    }

    public void exit() {
        while (!activityStack.isEmpty()) {
            Activity activity = activityStack.pop();
            activity.finish();
        }
        System.exit(0);
    }

}
